package com.fenghongzhang.day004;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class Main2Activity extends AppCompatActivity {

    private RadioGroup group;
    private RadioButton person;
    private RadioButton info;

    private PersonFragment personFragment;
    private InfoFragment infoFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        group = (RadioGroup) findViewById(R.id.group);
        person = (RadioButton) findViewById(R.id.person);
        info = (RadioButton) findViewById(R.id.info);


        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
         personFragment = new PersonFragment();
         infoFragment = new InfoFragment();
        fragmentTransaction.add(R.id.ll,personFragment);
        fragmentTransaction.add(R.id.ll,infoFragment);
        fragmentTransaction.hide(infoFragment);
        fragmentTransaction.commit();

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.person:
                        //让联系人的fg显示
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = manager.beginTransaction();
                        fragmentTransaction.show(personFragment);
                        fragmentTransaction.hide(infoFragment);
                        fragmentTransaction.commit();
                        break;
                    case R.id.info:
                        //让信息的fg显示
                        FragmentManager manager1 = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction1 = manager1.beginTransaction();
                        fragmentTransaction1.show(infoFragment);
                        fragmentTransaction1.hide(personFragment);
                        fragmentTransaction1.commit();
                        break;
                }
            }
        });


    }
}
